Reviewing the Stylebits Parameters
The four parameters of stylebits are AddBit, RemoveBit, AddExtendedBit, RemoveExtendedBit. For a review of these four parameters, and an introduction to Stylebits in general, please read [Stylebits Corner - An Introduction].
Graphic Windows and Scrollbars
A window opened for graphics automatically includes both a vertical and a horizontal scrollbar. Scrollbars are useful when a larger image is being used in a smaller space. The user simply scrolls to the desired area. This small snippet illustrates both a regular window and a graphics window.
Nomainwin
WindowWidth = 300
WindowHeight = 400
UpperLeftX = 50
UpperLeftY = 50
Open "A Regular Window" for Window as #w
#w, "Trapclose EndDemo"
UpperLeftX = Int(DisplayWidth/2) + 50
Open "A Graphics Window" for Graphics as #g
#g, "Trapclose EndDemo"
Wait
Sub EndDemo handle$
Close #w
Close #g
End
End Sub
Adding a Scrollbar to a Graphicbox
A graphicbox imbedded into a regular window lacks scrollbars. Graphics drawn or loaded beyond the viewing area of the graphicbox can not be seen. Stylebits allows scrollbars to be added to the graphicbox. The graphicbox now has the same functionality as the graphics window. Because the scrollbars consume graphicbox space, you will need to adjust the size of your graphicbox accordingly. An increase of 17 pixels works well.
Nomainwin
WindowWidth = 700
WindowHeight = 400
UpperLeftX = Int((DisplayWidth - WindowWidth)/2)
UpperLeftY = Int((DisplayHeight - WindowHeight)/2)
'Graphicbox #1 uses no stylebits
Graphicbox #w.g1, 20, 20, 300, 300
'Graphicbox #2 has scrollbars courtesy of stylebits
Stylebits #w.g2, _WS_VSCROLL or _WS_HSCROLL, 0, 0, 0
Graphicbox #w.g2, 350, 20, 317, 317
Open "Two Graphicboxes" for Window as #w
'Draw some graphics in each graphicbox
#w.g1, "Down; Fill Darkblue; Size 3"
#w.g2, "Down; Fill Darkblue; Size 3"
For i = 1 to 1000
redHue = Int(Rnd(1) * 256)
greenHue = Int(Rnd(1) * 256)
blueHue = Int(Rnd(1) * 256)
#w.g1, "Color ";redHue;" ";greenHue;" ";blueHue
#w.g2, "Color ";redHue;" ";greenHue;" ";blueHue
x1 = Int(Rnd(1) * 2000)
y1 = Int(Rnd(1) * 1200)
lngth = Int(Rnd(1) * 50) + 25
x2 = x1 + lngth
y2 = y1 + Int(Rnd(1) * lngth) + 10
#w.g1, "Line ";x1;" ";y1;" ";x2;" ";y2
#w.g2, "Line ";x1;" ";y1;" ";x2;" ";y2
#w.g1, "Flush"
#w.g2, "Flush"
Next i
Wait
Sub EndDemo handle$
Close #w
End
End Sub
Stylebits and API
There may be a time when you want to program the scrolling of scrollbars rather than waiting for the user to do a manual scroll. Scrollbars can easily be manipulated using the API call "SendMessageA" and a few Windows constants. This demo uses buttons to demonstrate a way to code scrolling. _SB_LINE scrolls 8 pixels while _SB_PAGE scrolls 64 pixels. These values may change depending upon other attributes you assign your window.
'Adding Scrollbars and Scrolling a Graphicbox
Nomainwin
WindowWidth = 600
WindowHeight = 400
UpperLeftX = Int((DisplayWidth - WindowWidth)/2)
UpperLeftY = Int((DisplayHeight - WindowHeight)/2)
GraphicBox #1.g, 0, 0, 500, 365
Stylebits #1.g, _WS_HSCROLL or _WS_VSCROLL, 0, 0, 0
Button #1.scrollUp, "Scroll Up", Scroll, UL, 515, 40, 62, 50
Stylebits #1.scrollUp, _BS_MULTILINE, 0, 0, 0
Button #1.scrollDown, "Scroll Down", Scroll, UL, 515, 120, 62, 50
Stylebits #1.scrollDown, _BS_MULTILINE, 0, 0, 0
Button #1.scrollLeft, "Scroll Left", Scroll, UL, 515, 200, 62, 50
Stylebits #1.scrollLeft, _BS_MULTILINE, 0, 0, 0
Button #1.scrollRight, "Scroll Right", Scroll, UL, 515, 280, 62, 50
Stylebits #1.scrollRight, _BS_MULTILINE, 0, 0, 0
Open "Scrolling a GraphicBox with API Call" for Window as #1
#1, "Trapclose EndDemo"
'Draw a grid
#1, "Font Times_New_Roman 12 Bold"
#1.g, "Down; Color Pink; Backcolor Buttonface; Fill Buttonface"
For x = 50 to 2000 Step 50
#1.g, "Line ";x;" 50 ";x;" 1250"
Next x
For y = 50 to 1200 Step 50
#1.g, "Line 50 ";y;" 2000 ";y
Next y
#1.g, "Color Black"
For x = 50 to 2000 Step 50
n = x - 20 + 5 * (x > 50) + 12 * (x = 50)
#1.g, "Place ";n;" 40"
#1.g, "\";x
Next x
For y = 100 to 1200 Step 50
n = 10 + 5 * (x > 50) + 12 * (x = 50)
#1.g, "Place ";n;" ";y + 4
#1.g, "\";y
Next y
#1.g, "Flush"
Wait
Sub EndDemo handle$
Close #1
End
End Sub
Sub Scroll handle$
Select Case
Case Instr(handle$, "Up") > 1
ScrollDirection = _WM_VSCROLL
ScrollAmount = _SB_PAGEUP 'or _SB_LINEUP
Case Instr(handle$, "Down") > 1
ScrollDirection = _WM_VSCROLL
ScrollAmount = _SB_PAGEDOWN 'or _SB_LINEDOWN
Case Instr(handle$, "Left") > 1
ScrollDirection = _WM_HSCROLL
ScrollAmount = _SB_PAGELEFT 'or _SB_LINELEFT
Case Instr(handle$, "Right") > 1
ScrollDirection = _WM_HSCROLL
ScrollAmount = _SB_PAGERIGHT 'or _SB_LINERIGHT
End Select
Call ScrollBox hWnd(#1.g), ScrollDirection, ScrollAmount
End Sub
Sub ScrollBox handle, ScrollDirection, ScrollAmount
Calldll #user32, "SendMessageA", _
handle as Ulong, _
ScrollDirection as Long, _
ScrollAmount as Long, _
0 as Long, _
result as Long
End Sub